package com.edbergogroup.springexamples.hibernate;

import java.util.List;


import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.NotYetImplementedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.edbergogroup.springexamples.domain.Person;

@Repository
public class HibernatePersonRepository extends HibernateDaoSupport implements PersonRepository {
	@Autowired
	  public void setupSessionFactory(SessionFactory sessionFactory) {
	    this.setSessionFactory(sessionFactory);
	  }
	@Override
	public List<Person> findByName(String firstName) {
		Query query = this.getSession().createQuery("SELECT p FROM Person p WHERE p.firstName = :firstName");
		query.setParameter("firstName", firstName);
		return (List<Person>) query.list();
		
	}

	@Override
	public Person findByID(Integer personId) throws DataAccessException {
		return (Person) this.getHibernateTemplate().load(Person.class, personId);
	}

	@Override
	public List<Person> findByCitty(String citty) {
		Query query = this.getSession().createQuery("SELECT p FROM Person p WHERE p.adress.citty = :citty");
		query.setParameter("citty", citty);
		return (List<Person>) query.list();
	}
	@Override
	public void save(Person person) {
		this.getHibernateTemplate().saveOrUpdate(person);
	}

}
